package dbz.domain;

import java.util.ArrayList;
import java.util.List;

import dbz.domain.deck.expceptions.EmptyDeckException;
import dbz.domain.field.Field;
import dbz.domain.player.Player;
import dbz.domain.turn.Turn;

public class Match {

	Field field;
	Player player1;
	Player player2;
	List<Turn> turns;
	Turn currentTurn;
	public static Match instance;

	public static Match getInstance(Player player1, Player player2) {
		if (instance == null) {
			instance = new Match(player1, player2);
		}
		return instance;
	}
	
	public static Match getInstance() {
		return instance;
	}

	private Match(Player player1, Player player2) {
		this.player1 = player1;
		this.player2 = player2;
		this.field = new Field();
		this.turns = new ArrayList<Turn>();
		initialize();
	}

	private void initialize() {
		try {
			player1.shuffleDeck();
			player2.shuffleDeck();
			player1.drawCards(5);
			player2.drawCards(5);
		} catch (EmptyDeckException e) {

		}

	}

	public Field getField() {
		return field;
	}

	public void setField(Field field) {
		this.field = field;
	}

	public Player getPlayer1() {
		return player1;
	}

	public void setPlayer1(Player player1) {
		this.player1 = player1;
	}

	public Player getPlayer2() {
		return player2;
	}

	public void setPlayer2(Player player2) {
		this.player2 = player2;
	}

	public List<Turn> getTurns() {
		return turns;
	}

	public void setTurns(List<Turn> turns) {
		this.turns = turns;
	}

	public Turn getCurrentTurn() {
		return currentTurn;
	}

	public void setCurrentTurn(Turn currentTurn) {
		this.currentTurn = currentTurn;
	}

	public Integer getTurnNumber() {
		return turns.size() + 1;
	}
	
	public void endCurrentTurn() {
		turns.add(currentTurn);
		currentTurn = new Turn(this,
				currentTurn.getPassivePlayer(),
								currentTurn.getActivePlayer(),
								currentTurn.getDefensiveFighter(),
								currentTurn.getAttackingFighter());
		
	}

}
